package com.xiaoge.basedevlibrary.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;

/**
 *@Description:日期工具类
 *@Author:张聪
 *@Since:2015年1月10日下午3:21:52
 */
@SuppressLint("SimpleDateFormat")
public class DateUtils {

    private static SimpleDateFormat formatYMD      = new SimpleDateFormat ("yyyy-MM-dd");

    private static SimpleDateFormat formatYYYYMMDD = new SimpleDateFormat ("yyyyMMdd");

    private DateUtils() {

    }

    /**
     * 获取当前时间对应的时间戳
     * @return
     */
    public static String getNowTimeStamp(){
        Date now = new Date ();
        DateFormat format = new SimpleDateFormat ("yyyyMMddHHmmssSSS");
        return format.format (now);
    }

    /**
     * 获取最后更新时间
     * @return
     */
    public static String getLastUpdateTime(){
        return new SimpleDateFormat ("HH:mm").format (new Date ());
    }

    /**
     * 
    * @Title: isAfter 
    * @Description: TODO(判断当前是否是否在预约起点时间之后) 
    * @param @param serviceBeginTime
    * @param @return    
    * @return boolean    
    * @throws
     */
    public static boolean isAfter(String serviceBeginTime){
        try {
            DateFormat format = new SimpleDateFormat ("yyyyMMddHHmmss");
            return System.currentTimeMillis () > format.parse (serviceBeginTime).getTime ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return false;
    }

    /**
     * 转换预约时间
     * @param serviceBeginTime
     * @param serviceEndTime
     * @return
     */
    public static String convertHopeTime(String serviceBeginTime,String serviceEndTime){
        String resultStr = "";
        DateFormat format = new SimpleDateFormat ("yyyyMMddHHmmss");
        DateFormat format1 = new SimpleDateFormat ("yyyyMMdd");
        DateFormat format2 = new SimpleDateFormat ("HH:mm");
        DateFormat format3 = new SimpleDateFormat ("MM月dd日");
        try {
            Date beginDate = format.parse (serviceBeginTime);
            Date endDate = format.parse (serviceEndTime);
            Date today = new Date ();
            Calendar calendar = Calendar.getInstance ();
            calendar.setTime (today);
            calendar.add (Calendar.DAY_OF_MONTH, 1);
            Date tommorow = calendar.getTime ();
            String dateStr = "";
            if (format1.format (beginDate).equals (format1.format (today))) {
                dateStr = "今天";
            } else if (format1.format (beginDate).equals (format1.format (tommorow))) {
                dateStr = "明天";
            } else {
                dateStr = format3.format (beginDate);
            }
            String beginTime = format2.format (beginDate);
            String endTime = format2.format (endDate);
            resultStr = dateStr + " " + beginTime + "-" + endTime;
        } catch (ParseException e) {}
        return resultStr;
    }

    /**
     * 
    * @Title: convertTimeByMMdd 
    * @Description: TODO(获取时间,日期格式) 
    * @param @param time
    * @param @return    
    * @return String    
    * @throws
     */
    public static String convertTimeByMMdd(long time){
        SimpleDateFormat format = new SimpleDateFormat ("MM-dd");
        return format.format (new Date (time));
    }

    /**
     * 
    * @Title: convertTimeByYYYYMMDD 
    * @Description: TODO(转换时间格式yyyy.MM.dd HH:mm) 
    * @param @param time
    * @param @return    
    * @return String    
    * @throws
     */
    public static String convertTimeByYYYYMMDD(long time){
        SimpleDateFormat format = new SimpleDateFormat ("yyyy.MM.dd HH:mm");
        return format.format (new Date (time));
    }

    /**
     * 
    * @Title: convertTimeByHHmm 
    * @Description: TODO(获取时间,时间格式) 
    * @param @param time
    * @param @return    
    * @return String    
    * @throws
     */
    public static String convertTimeByHHmm(long time){
        SimpleDateFormat format = new SimpleDateFormat ("HH:mm");
        return format.format (new Date (time));
    }

    /**
     *@Description:转换时间格式
     *@Author: 李焱生
     *@Since: 2015年4月9日下午1:27:38
     *@param trimNull
     *@return
     */
    public static String convertTime(String time){
        if (StringUtils.isEmpty (time)) { return ""; }
        DateFormat format1 = new SimpleDateFormat ("yyyyMMddHHmmss");
        DateFormat format2 = new SimpleDateFormat ("yyyy.MM.dd HH:mm");
        try {
            return format2.format (format1.parse (time));
        } catch (ParseException e) {}
        return "";
    }

    public static boolean compareTime(String starttime){
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss");
        Date nowdate = new Date ();
        boolean warmflag = false;
        try {
            Date startdt = sdf.parse (starttime);

            if ((startdt.getDate () == nowdate.getDate ()) && (startdt.getHours () == nowdate.getHours ())) {
                warmflag = true;
            }
        } catch (ParseException e) {
            e.printStackTrace ();
        }
        return warmflag;
    }

    /**
     *@Description:计算两个时间之间的相差多少分钟
     *@Author: 张聪
     *@Since: 2015年8月4日上午11:13:24
     *@param endDate 结束时间
     *@param nowDate 开始时间
     *@return
     */
    public static long timeDifference(Date endDate,Date nowDate){
        if (null == endDate || null == nowDate) {
            return 0;
        } else {
            // long nm = 1000 * 60;
            // 获得两个时间的毫秒时间差异
            return endDate.getTime () - nowDate.getTime ();
            // long diff = endDate.getTime () - nowDate.getTime ();
            // 计算差多少分钟
            // BigDecimal divisor = new BigDecimal (diff);
            // BigDecimal dividend = new BigDecimal (nm);
            // double min = divisor.divide (dividend, 2, BigDecimal.ROUND_HALF_UP).doubleValue ();
            // return String.valueOf (min);

        }
    }

    /**
     *@Description: 格式化日期字符串
     *@Author: 张聪
     *@Since: 2015年11月3日下午3:55:38
     *@param time
     *@return
     */
    public static String dateFormatTime(String time){
        if (StringUtils.isEmpty (time)) { return ""; }
        DateFormat format1 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        DateFormat format2 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        try {
            return format2.format (format1.parse (time));
        } catch (ParseException e) {}
        return "";
    }

    /**
     * @Title: getDateByamount
     * @Description: 获取 date 日期的前或后N天
     * @Author:张聪
     * @param date
     * @param amount N为正数：表示加N天； N为负数：表示减N天
     * @return 参数
     * @return String 返回类型
     * @throws
     */

    public static String getDateByamount(Date date,int amount){
        Calendar calendar = Calendar.getInstance ();
        calendar.setTime (date);
        calendar.add (Calendar.DAY_OF_YEAR, amount);
        return formatYYYYMMDD.format (calendar.getTime ());
    }
}
